package com.iplay.web.cashback;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iplay.dto.cashback.CashbackStrategyDTO;
import com.iplay.service.cashback.CashbackService;
import com.iplay.vo.cashback.CashbackStrategyVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/global_cashback_strategy")
public class CashbackController {
	
	@Autowired
	private CashbackService cashbackService;

	@ApiOperation(notes = "管理员获得默认的返现策略，返回值可能为null<br>"
			+ "返现比例中的percentageForCustomer(double)代表百分比中的数值，例如percentageForCustomer=0.3，表示客户获得的返现比例为0.3%<br>", value = "")
	@GetMapping
	@PreAuthorize("hasRole('ADMIN')")
	public CashbackStrategyDTO getGlobalCashbackStrategy(){
		return cashbackService.findGlobalCashbackStrategy().orElse(null);
	}
	
	@ApiOperation(notes = "管理员修改默认的返现策略<br>"
			+ "返现比例中的percentageForCustomer(double)代表百分比中的数值，例如percentageForCustomer=0.3，表示客户获得的返现比例为0.3%<br>", value = "")
	@PutMapping
	@PreAuthorize("hasRole('ADMIN')")
	public boolean updateGlobalCashbackStrategy(@RequestBody CashbackStrategyVO cashbackStrategy){
		return cashbackService.setDefaultCashbackStrategy(cashbackStrategy);
	}
	
}
